package ae.pcfc.cldos.online.common.facade.provider;

import java.util.Date;

import org.springframework.transaction.annotation.Transactional;

import ae.pcfc.cldos.online.common.clddb.domain.AppMetaData;
import ae.pcfc.cldos.online.common.clddb.domain.ApplicationStatus;
import ae.pcfc.cldos.online.common.clddb.domain.EmployeeDetail;
import ae.pcfc.cldos.online.common.clddb.domain.EmploymentVisaCancel;
import ae.pcfc.cldos.online.common.clddb.domain.TaskMaster;
import ae.pcfc.cldos.online.common.clddb.domain.manager.AppMetaDataDOM;
import ae.pcfc.cldos.online.common.clddb.domain.manager.EmployeeDetailDOM;
import ae.pcfc.cldos.online.common.clddb.domain.manager.EmploymentVisaCancelDOM;
import ae.pcfc.cldos.online.common.etksdb.domain.manager.ClientDOM;
import ae.pcfc.cldos.online.common.exception.NoOnlineClientException;
import ae.pcfc.cldos.online.common.facade.BaseFacade;
import ae.pcfc.cldos.online.common.facade.GsIntegrationFacade;
import ae.pcfc.cldos.online.common.manager.TaskAndNotificationsManager;
import ae.pcfc.cldos.online.common.tscdb.domain.manager.TTscClientCompanyDOM;
import ae.pcfc.cldos.online.common.vo.AppMetaDataVO;
import ae.pcfc.cldos.online.common.vo.employee.details.EmployeeDetailsVO;

/**
 *
 * @author arosa
 *
 */
public class GsIntegrationFacadeImpl extends BaseFacade implements GsIntegrationFacade {

	
	private ClientDOM clientDOM;
	
	public void setClientDOM(ClientDOM clientDOM) {
		this.clientDOM = clientDOM;
	}
	
	private EmployeeDetailDOM employeeDetailDOM;
	
	public void setEmployeeDetailDOM(EmployeeDetailDOM employeeDetailDOM) {
		this.employeeDetailDOM = employeeDetailDOM;
	}
	
	private AppMetaDataDOM appMetaDataDOM;
	
	public void setAppMetaDataDOM(AppMetaDataDOM appMetaDataDOM) {
		this.appMetaDataDOM = appMetaDataDOM;
	}
	
	private TaskAndNotificationsManager taskAndNotificationsManager;
	
	public void setTaskAndNotificationsManager(
			TaskAndNotificationsManager taskAndNotificationsManager) {
		this.taskAndNotificationsManager = taskAndNotificationsManager;
	}
	
	private TTscClientCompanyDOM tTscClientCompanyDOM;
	
	public void settTscClientCompanyDOM(
			TTscClientCompanyDOM tTscClientCompanyDOM) {
		this.tTscClientCompanyDOM = tTscClientCompanyDOM;
	}
	
	private EmploymentVisaCancelDOM employmentVisaCancelDOM;
	
	public void setEmploymentVisaCancelDOM(
			EmploymentVisaCancelDOM employmentVisaCancelDOM) {
		this.employmentVisaCancelDOM = employmentVisaCancelDOM;
	}
	
	@Transactional(rollbackFor=Exception.class)
	public void startTask(AppMetaDataVO appMetaDataVO, EmployeeDetailsVO employeeDetailsVO){
		
		AppMetaData appMetaData = appMetaDataDOM.getApplicationByReferenceNumber(appMetaDataVO.getClientRefNo());
		
		if(appMetaData==null){
			appMetaData = new AppMetaData();
			getMapper().map(appMetaDataVO, appMetaData);
			appMetaData.setAppComments("OFFLINE");
			appMetaData.setApplicationDate(new Date());
			String trakheesId = tTscClientCompanyDOM.getCompanyTrakheesIdByLicanseNo(appMetaDataVO.getCoLicNo());
			Long clientId = clientDOM.getClientIdByTrakheesId(trakheesId);
			appMetaData.setClientId(clientId);
			appMetaDataDOM.add(appMetaData);
			appMetaDataDOM.addEtksAppMetaData(appMetaData);	
		} else {
			appMetaData.setStatusCode(appMetaDataVO.getStatusCode());
			appMetaDataDOM.update(appMetaData);
		}
		
		if(appMetaData.getStatusCode().equals(ApplicationStatus.PAYMENT_RENEWAL) ||
				appMetaData.getStatusCode().equals(ApplicationStatus.PAYMENT_CANCELATION)){
			EmployeeDetail employeeDetail = appMetaData.getEmployeeDetail();
			if(employeeDetail==null){
				employeeDetail = new EmployeeDetail();
				getMapper().map(employeeDetailsVO, employeeDetail);
				employeeDetail.setAppMetaData(appMetaData);
				employeeDetailDOM.add(employeeDetail);
			}
		} else if (appMetaData.getStatusCode().equals(ApplicationStatus.STATUS_EXIT_PAPER_SUBMIT)){
			EmploymentVisaCancel employmentVisaCancel = appMetaData.getEmploymentVisaCancel();
			if(employmentVisaCancel==null){
				employmentVisaCancel = new EmploymentVisaCancel();
				employmentVisaCancel.setAppMetaData(appMetaData);
				employmentVisaCancel.setEvcEmployeeId(employeeDetailsVO.getEmployeeId());
				employmentVisaCancelDOM.add(employmentVisaCancel);
			}
		}
		taskAndNotificationsManager.sendTask(appMetaData, TaskMaster.__ADMIN_APPROVE_TASK_CODE);
		
	}
	
	@Transactional
	public Long getOnlineApplicationId(AppMetaDataVO appMetaDataVO) throws NoOnlineClientException{
		AppMetaData appMetaData = appMetaDataDOM.getApplicationByReferenceNumber(appMetaDataVO.getClientRefNo());
		
		if(appMetaData==null){
			appMetaData = new AppMetaData();
			appMetaData.setApplicationDate(new Date());
			appMetaData.setAppComments("OFFLINE");
			String trakheesId = tTscClientCompanyDOM.getCompanyTrakheesIdByLicanseNo(appMetaDataVO.getCoLicNo());
			Long clientId = clientDOM.getClientIdByTrakheesId(trakheesId);
			if(clientId==null){
				throw new NoOnlineClientException("No online client for this company");
			}
			appMetaData.setClientId(clientId);
			appMetaData.setWfIncidentId(appMetaDataVO.getWfIncidentId());
			appMetaData.setWfInitiatedBy(appMetaDataVO.getWfInitiatedBy());
			appMetaData.setClientRefNo(appMetaDataVO.getClientRefNo());
			appMetaData.setProcessCode(appMetaDataVO.getProcessCode());
			appMetaData.setServiceCode(appMetaDataVO.getServiceCode());
			appMetaData.setStatusCode(ApplicationStatus.UNKNOWN_STATUS);
			appMetaDataDOM.add(appMetaData);
		} 
		
		return appMetaData.getApplicationId();
	}
}
